package cn.tedu._05springmvc01.controller;

import cn.tedu._05springmvc01.pojo.dto.UserRegDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {
    /**
     * @return
     * @RequestMapping("/login") 映射请求路径
     * @ResponseBody 响应数据
     */
    @RequestMapping("/login")
    @ResponseBody
    public String login() {
        return "登录成功";
    }

    /**
     * GET 请求
     * http://localhost:8080/register?username=xxx&password=yyy
     *
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public String register() {
        return "注册成功";
    }

    /**
     * 接收用户数据方式1:通过HttpServletRequest对象接收
     * http://localhost:8080/regParam1?username=xxx&password=yyy
     */
    @RequestMapping("/regParam1")
    @ResponseBody
    public String regParam1(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        return username + ":" + password;
    }

    /**
     * 接收用户数据方式2:通过POJO类[DTO类]接收
     */
    @RequestMapping("/regParam2")
    @ResponseBody
    public String regParam2(UserRegDTO userRegDTO) {
        String username = userRegDTO.getUsername();
        String password = userRegDTO.getPassword();
        return username + ":" + password;
    }

    /**
     * 接收用户数据方式3:通过声明参数方式接收
     */
    @RequestMapping("/regParam3")
    @ResponseBody
    public String regParam3(String username, String password) {
        return username + ":" + password;
    }

}
